"""
生成历史指数数据
使用：
    准备好指数的成分品种
    准备好成分的比例数据，类似于：industrial_for_self_data.csv
存在问题：
    对于品种加入之前时间的数据没有自动去提
"""
import pandas as pd
import rqdatac as rq
industrial = ['RU', 'AL', 'CU', 'FU', 'TA', 'ZN', 'L', 'RB', 'V', 'PB', 'J', 'FG', 'JM', 'BU', 'I', 'PP', 'HC', 'MA',
              'SF', 'SM', 'NI', 'SN', 'SC', 'SP', 'EG', 'UR', 'NR', 'SS', 'EB', 'SA', 'PG', 'LU', 'PF', 'SI', 'AO',
              'BR', 'LC', 'PX', 'SH', "ZC"]

rq.init("13570866213", "39314656")

ind_sec = [sec+"99" for sec in industrial]
prices = rq.get_price(ind_sec, frequency="1m", fields="close", start_date="20200101", end_date="20240924",expect_df=False)
from datetime import datetime, timedelta
timedelta1 = timedelta(minutes=1)

prices.index = prices.index - timedelta1

change_date = datetime(2022,8,19, 15,0,0)

rates = pd.read_csv("E:\daily work\文华商品指数\industrial\\industrial_for_self_data.csv")
r0 = rates.loc[0]
r0 = r0.to_dict()
r0.pop("date")
r1 = rates.loc[1]
r1 = r1.to_dict()

r1.pop("date")

sec_point = {}
for sec in industrial:
    sec_r0 = r0.get(sec.lower())
    sec_r1 = r1.get(sec.lower())
    sec_p = prices[sec + "99"] * sec_r0
    sec_p.loc[change_date:] = prices[sec + "99"] * sec_r1
    sec_point[sec] = sec_p

df = pd.DataFrame(sec_point)
df.ffill(inplace=True)
df.to_csv("E:\daily work\文华商品指数\industrial\\self_made_industrial_1m_20230925.csv")
